home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / StringVector.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  990 b   |  39 lines

  1. //: C20:StringVector.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // A vector of strings
  7. #include "../require.h"
  8. #include <string>
  9. #include <vector>
  10. #include <fstream>
  11. #include <iostream>
  12. #include <iterator>
  13. #include <sstream>
  14. using namespace std;
  15.  
  16. int main(int argc, char* argv[]) {
  17.   requireArgs(argc, 1);
  18.   ifstream in(argv[1]);
  19.   assure(in, argv[1]);
  20.   vector<string> strings;
  21.   string line;
  22.   while(getline(in, line))
  23.     strings.push_back(line);
  24.   // Do something to the strings...
  25.   int i = 1;
  26.   vector<string>::iterator w;
  27.   for(w = strings.begin();
  28.       w != strings.end(); w++) {
  29.     ostringstream ss;
  30.     ss << i++;
  31.     *w = ss.str() + ": " + *w;
  32.   }
  33.   // Now send them out:
  34.   copy(strings.begin(), strings.end(),
  35.     ostream_iterator<string>(cout, "\n"));
  36.   // Since they aren't pointers, string 
  37.   // objects clean themselves up! 
  38. } ///:~
  39.